Skip to content

Fix for side-effects seeping into data query#419

Merged
teunbrand merged 10 commits intoposit-dev:mainfrom
teunbrand:fix_create_temp_table_global_query
May 4, 2026
Merged

Fix for side-effects seeping into data query#419
teunbrand merged 10 commits intoposit-dev:mainfrom
teunbrand:fix_create_temp_table_global_query

Conversation

@teunbrand
Copy link
Copy Markdown
Collaborator

@teunbrand teunbrand commented May 1, 2026

This PR aims to fix #415.

The transform_global_sql() was wrapping the user-provided query like so:

CREATE TABLE "__ggsql_global_..." AS CREATE TEMP TABLE data(x, y) AS (VALUE...

Whereas now

  • CREATE TEMP TABLE data(x, y) AS (VALUE... is executed seperately as a side-effect
  • CREATE TABLE "__ggsql_global_..." AS SELECT * FROM data becomes the global data query

I'm mainly concerned about Chesterton's fence: I don't really know why CREATE statements were transformed in the first place. So that'd be a good item to review.

teunbrand and others added 5 commits May 1, 2026 14:53
transform_global_sql concatenated side-effect SQL (CREATE, INSERT, …) with
the VISUALISE FROM injection, then the caller wrapped the whole thing in
another CREATE TABLE AS, producing invalid SQL like:
  CREATE TABLE __ggsql_global__ AS CREATE TEMP TABLE data …

Split the return into side-effect statements (executed directly) and the
queryable part (wrapped as the global temp table).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@teunbrand teunbrand marked this pull request as ready for review May 1, 2026 14:33
@teunbrand teunbrand requested a review from thomasp85 May 1, 2026 14:33
@thomasp85
Copy link
Copy Markdown
Collaborator

I think this is old unneeded bagage that you are removing so no need for concern (and Claude agrees).

A few comments but otherwise good

  • The added test only covers one CREATE before VISUALISE FROM — a test with CREATE ...; INSERT ...; VISUALISE FROM ... would ensure we correctly handle multi-statement queries correctly.
  • A query with both side effects and a trailing SELECT will still short-circuit on the SELECT and skip the side effects. That's pre-existing behavior, not a regression — but worth flagging (flagged by Claude).
  • does_consume_cte() seems somewhat mis-named now.

teunbrand and others added 5 commits May 4, 2026 11:25
The tree-sitter lexer was misclassifying INSERT, UPDATE, and DELETE
keywords as bare_identifier tokens, causing them to fall through to
other_sql_statement instead of their specific statement rules. This
happened because keyword prefixes (e.g. IN for INSERT) derailed the
lexer's specific keyword path.

Fix: wrap the three leading keywords in token(prec(1, ...)) so they
get dedicated high-priority lexer tokens, and downgrade
other_sql_statement's catch-all regex from token() to a bare regex.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
transform_global_sql returned empty side-effects when a SELECT was
present, so CREATE/INSERT preambles were skipped. Hoist side-effect
extraction so it runs regardless of the query type.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@teunbrand
Copy link
Copy Markdown
Collaborator Author

a test with CREATE ...; INSERT ...; VISUALISE FROM ... would ensure we correctly handle multi-statement queries correctly.

It is good that you asked, because it exposed a bug in the grammar, where INSERT/DELETE/UPDATE etc weren't parsed as their insert_statement nodes.

A query with both side effects and a trailing SELECT will still short-circuit on the SELECT and skip the side effects. That's pre-existing behavior, not a regression — but worth flagging (flagged by Claude).

Addressed in b6805de

does_consume_cte() seems somewhat mis-named now.

Renamed it has_executable_sql()

@teunbrand teunbrand merged commit 5912bb7 into posit-dev:main May 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow for CREATE TEMP TABLE

2 participants